package com.tianbo.elfii.client.common;

import java.io.*;
import java.sql.* ;
import java.net.*;
import java.awt.*;
import java.util.*;
import java.util.Timer;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.*;
import javax.imageio.*;

import com.tianbo.elfii.client.common.ElfiiCommonConfig.LightVolumeWindowPanel;
import com.tianbo.elfii.client.common.ElfiiCommonConfig.SonicVolumeWindowPanel;
import com.tianbo.elfii.client.common.ElfiiCommonUtil.FeedBackType;

/**  <pre>
     <b>类功能描述：</b>
     完成 Config-关于本机 部分功能要求。
     </pre>
     @version 1.0.1
     @author 高伟 
*/

public class ElfiiCommonConfigAboutMachine {
    JPanel configPanel;
    JPanel itemPanel;

    int infoBgStartX0;
    int infoBgStartY0;

    int dataChoiceIndex;

    boolean isAboutMachineSelected=false;
    boolean isWifiSettingSelected=false;

    JFrame mainFrame;
    ElfiiCommonRunInfo runInfo;

    ItemButtonPanel namePanel;
    ItemButtonPanel versionPanel;
    ItemButtonPanel modelPanel;
    ItemButtonPanel serialPanel;
    ItemButtonPanel netAddressPanel;
    ItemButtonPanel wifiAddressPanel;
    ItemButtonPanel lawPanel;
    ItemButtonPanel permittionPanel;

    final int Id_namePanel=1;
    final int Id_versionPanel=2;
    final int Id_modelPanel=3;
    final int Id_serialPanel=4;
    final int Id_netAddressPanel=5;
    final int Id_wifiAddressPanel=6;
    final int Id_lawPanel=7;
    final int Id_permittionPanel=8;

    int selectedId;
    
    JButton okButton;
    
    JPanel mainPanel;
    
    LicenseWindowPanel licenseWindowPanel;
    LawWindowPanel lawWindowPanel;

    public void setRunInfo(ElfiiCommonRunInfo runInfo){
        this.runInfo=runInfo;
        this.mainFrame=runInfo.getMainFrame();
    }

    public void dealBackButton(){
        mainFrame.getContentPane().removeAll();
        ElfiiCommonConfig config = new ElfiiCommonConfig();
        config.setRunInfo(runInfo);
        JPanel p=config.createViewPanel();
        mainFrame.getContentPane().setLayout(new BorderLayout());
        mainFrame.getContentPane().add(p,BorderLayout.CENTER);
        mainFrame.validate();
    }

    public void dealOkButton(){

        if (selectedId==Id_lawPanel) {
        	
        	lawWindowPanel.screenshot();
        	mainFrame.setContentPane(lawWindowPanel);
        	mainFrame.validate();
        }
        
        if (selectedId==Id_permittionPanel) {
        	
        	licenseWindowPanel.screenshot();
        	mainFrame.setContentPane(licenseWindowPanel);
        	mainFrame.validate();

        }
    }

    class LawWindowPanel extends JPanel {

        BufferedImage infoImage;
        BufferedImage bgImg=null;
        BufferedImage bgImg2;

        public LawWindowPanel(){
            File f;
            try {
                f=new File("resource-common/images/config/icons/tb.png");
                infoImage=ImageIO.read(f);
                
                f=new File("resource-common/images/config/bg.png");
                bgImg2 = ImageIO.read(f);

            } catch(Exception e) {System.out.print(e);}
        }
        
        public void screenshot(){
            try {
                int x0=(int) mainFrame.getLocationOnScreen().getX();
                int y0=(int) mainFrame.getLocationOnScreen().getY();
                Robot rbt = new Robot();
                bgImg=rbt.createScreenCapture(new Rectangle(x0,y0,1024,768));
                
            } catch(Exception e) {System.out.print(e);}
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g ;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
//            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f));   
            g2.drawImage(bgImg,null,0,0);
            g2.drawImage(bgImg2,null,0,0);
//            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));   

            int rectX0=242,rectY0=220;
            int rectWidth=530,rectHeight=280;

            g2.setColor(Color.white);
            g2.fillRoundRect(rectX0,rectY0,rectWidth,rectHeight,10,10);
            g2.setColor(Color.black);
            g2.fillRoundRect(rectX0+5,rectY0+5,rectWidth-10,rectHeight-10,10,10);
            g2.drawImage(infoImage,null,rectX0+30,rectY0+20);
            g2.setColor(Color.gray);
            g2.drawLine(rectX0+20,rectY0+85,rectX0+rectWidth-20,rectY0+85);
            g2.fillRect(rectX0+5,rectY0+rectHeight-55,rectWidth-10,50);

            g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,30));
            g2.setColor(Color.white);
            g2.drawString("法律声明",rectX0+115,rectY0+60);
            g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,16));
            g2.drawString("本声明....",rectX0+110,rectY0+130);

        }
        public Dimension getPreferredSize() {
            return (new Dimension(300,100));
        }
    }
    
    class LicenseWindowPanel extends JPanel {

        BufferedImage infoImage;
        BufferedImage bgImg=null;
        BufferedImage bgImg2;

        public LicenseWindowPanel(){
            File f;
            try {
                f=new File("resource-common/images/config/icons/tb.png");
                infoImage=ImageIO.read(f);
                
                f=new File("resource-common/images/config/bg.png");
                bgImg2 = ImageIO.read(f);
            } catch(Exception e) {System.out.print(e);}
        }
        
        public void screenshot(){
            try {
                int x0=(int) mainFrame.getLocationOnScreen().getX();
                int y0=(int) mainFrame.getLocationOnScreen().getY();
                Robot rbt = new Robot();
                bgImg=rbt.createScreenCapture(new Rectangle(x0,y0,1024,768));
                
            } catch(Exception e) {System.out.print(e);}

        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g ;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
//            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f));   
            g2.drawImage(bgImg,null,0,0);
//            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));   
            g2.drawImage(bgImg2,null,0,0);
            
            int rectX0=242,rectY0=220;
            int rectWidth=530,rectHeight=280;

            g2.setColor(Color.white);
            g2.fillRoundRect(rectX0,rectY0,rectWidth,rectHeight,10,10);
            g2.setColor(Color.black);
            g2.fillRoundRect(rectX0+5,rectY0+5,rectWidth-10,rectHeight-10,10,10);
            g2.drawImage(infoImage,null,rectX0+30,rectY0+20);
            g2.setColor(Color.gray);
            g2.drawLine(rectX0+20,rectY0+85,rectX0+rectWidth-20,rectY0+85);
            g2.fillRect(rectX0+5,rectY0+rectHeight-55,rectWidth-10,50);

            g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,30));
            g2.setColor(Color.white);
            g2.drawString("许可证",rectX0+115,rectY0+60);
            g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,16));
            g2.drawString("本声明....",rectX0+110,rectY0+130);
        }
        
        public Dimension getPreferredSize() {
            return (new Dimension(300,100));
        }
    }


    public void createConfigPanel(){

        JButton aboutMachineButton=new JButton("关于本机");
        aboutMachineButton.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,15));
        aboutMachineButton.setBackground(Color.gray);
        aboutMachineButton.setForeground(Color.white);
        aboutMachineButton.setBorderPainted(false);
        aboutMachineButton.setFocusPainted(false);
        aboutMachineButton.setHorizontalAlignment(SwingConstants.LEFT);
        aboutMachineButton.setModel(new DefaultButtonModel() {
                public boolean isPressed(){
                    return false; 
                }
            }
            ) ;


        namePanel=new ItemButtonPanel("名称","爱飞飞（ELFii）全脑开发机");
        namePanel.setFocusable(true);
        namePanel.setBackground(Color.black);
        namePanel.addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e) {
                    setSelectButton(Id_namePanel);
                }
            }
            );

        versionPanel=new ItemButtonPanel("版本","V"+ElfiiCommonConfigUtil.getSwVersion());
        versionPanel.setFocusable(true);
        versionPanel.setBackground(Color.black);
        versionPanel.addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e) {
                    setSelectButton(Id_versionPanel);
                }
            }
            );

        modelPanel=new ItemButtonPanel("型号","ELFII_01");
        modelPanel.setFocusable(true);
        modelPanel.setBackground(Color.black);
        modelPanel.addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e) {
                    setSelectButton(Id_modelPanel);
                }
            }
            );

        serialPanel=new ItemButtonPanel("序列号",ElfiiCommonConfigUtil.getProductSN());
        serialPanel.setFocusable(true);
        serialPanel.setBackground(Color.black);
        serialPanel.addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e) {
                    setSelectButton(Id_serialPanel);
                }
            }
            );

        netAddressPanel=new ItemButtonPanel("以太网卡地址",ElfiiCommonConfigUtil.getLanSN());
        netAddressPanel.setFocusable(true);
        netAddressPanel.setBackground(Color.black);
        netAddressPanel.addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e) {
                    setSelectButton(Id_netAddressPanel);
                }
            }
            );

        wifiAddressPanel=new ItemButtonPanel("Wi-Fi地址",ElfiiCommonConfigUtil.getWlanSN());
        wifiAddressPanel.setFocusable(true);
        wifiAddressPanel.setBackground(Color.black);
        wifiAddressPanel.addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e) {
                    setSelectButton(Id_wifiAddressPanel);
                }
            }
            );

        lawPanel=new ItemButtonPanel("法律声明","本机器包含机器使用的有关条款。");
        lawPanel.setFocusable(true);
        lawPanel.setBackground(Color.black);
        lawPanel.addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e) {
                    setSelectButton(Id_lawPanel);
                }
            }
            );

        permittionPanel=new ItemButtonPanel("许可证","符合国际质量体系认证...");
        permittionPanel.setFocusable(true);
        permittionPanel.setBackground(Color.black);
        permittionPanel.addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e) {
                    setSelectButton(Id_permittionPanel);
                }
            }
            );

        configPanel=new JPanel();
        configPanel.setBackground(Color.black);
        configPanel.setLayout(null);

        configPanel.add(aboutMachineButton);
        aboutMachineButton.setBounds(0,10,1024,25);

        configPanel.add(namePanel);
        namePanel.setBounds(0,35,1024,65);
        configPanel.add(versionPanel);
        versionPanel.setBounds(0,100,1024,65);
        configPanel.add(modelPanel);
        modelPanel.setBounds(0,165,1024,65);
        configPanel.add(serialPanel);
        serialPanel.setBounds(0,230,1024,65);
        configPanel.add(netAddressPanel);
        netAddressPanel.setBounds(0,295,1024,65);
        configPanel.add(wifiAddressPanel);
        wifiAddressPanel.setBounds(0,360,1024,65);
        configPanel.add(lawPanel);
        lawPanel.setBounds(0,425,1024,65);
        configPanel.add(permittionPanel);
        permittionPanel.setBounds(0,490,1024,65);

    }

    public void setSelectButton(int selectedId){
    	
    	if(this.selectedId==selectedId){
    		return;
    	}
    	
        this.selectedId=selectedId;
        namePanel.setSelected(false);
        versionPanel.setSelected(false);
        modelPanel.setSelected(false);
        serialPanel.setSelected(false);

        netAddressPanel.setSelected(false);
        wifiAddressPanel.setSelected(false);
        lawPanel.setSelected(false);
        permittionPanel.setSelected(false);


        if (selectedId==Id_namePanel)
            namePanel.setSelected(true);
        if (selectedId==Id_versionPanel)
            versionPanel.setSelected(true);
        if (selectedId==Id_modelPanel)
            modelPanel.setSelected(true);
        if (selectedId==Id_serialPanel)
            serialPanel.setSelected(true);
        if (selectedId==Id_netAddressPanel)
            netAddressPanel.setSelected(true);
        if (selectedId==Id_wifiAddressPanel)
            wifiAddressPanel.setSelected(true);
        
        okButton.setVisible(false);
        if (selectedId==Id_lawPanel){
            lawPanel.setSelected(true);
            okButton.setVisible(true);
        }
        if (selectedId==Id_permittionPanel){
            permittionPanel.setSelected(true);
            okButton.setVisible(true);
        }
        ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
    }

    class ItemButtonPanel extends JPanel {
        BufferedImage selectedBgImage;

        BufferedImage lineImage;

        BufferedImage img2;

        BufferedImage gaoImg;

        String str1;
        String str2;
        boolean isSelected;
        public ItemButtonPanel(String str1,
                               String str2) {
            this.str1=str1;
            this.str2=str2;
            File f=null;
            try {
                f=new File("resource-common/images/config/icons/tc_butbg.png");
                selectedBgImage=ImageIO.read(f);
                f=new File("resource-common/images/config/icons/tb_bg.png");
                img2=ImageIO.read(f);
                f=new File("resource-common/images/config/icons/line.png");
                lineImage=ImageIO.read(f);
            } catch(Exception e) {System.out.print(e+"   "+f);}
        }

        public void setSelected(boolean isSelected){
            this.isSelected=isSelected;
            repaint();
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2 = (Graphics2D) g ;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
            g2.setColor(Color.black);
            g2.fillRect(0,0,1024,58);
            if (isSelected) {
            	g2.setColor(ElfiiCommonConstant.Elfii_Blue);
                g2.drawImage(selectedBgImage,null,0,0);
                //g2.drawImage(img2,null,0,0);
                //g2.drawImage(selectedImage,null,10,0);
            }else{

                g2.setColor(Color.white);
                //g2.drawImage(img2,null,10,0);
                //g2.drawImage(unSelectedImage,null,10,0);
            }

            g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,20));
            g2.drawString(str1,20,25);  
            g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,14));
            g2.drawString(str2,20,50);  
            g2.drawImage(lineImage,null,0,64);
        }

        public Dimension getPreferredSize() {
            return (new Dimension(1024,66));
        }
    }

    public JPanel createViewPanel(){
    	mainPanel = new JPanel(new BorderLayout()) ;
        okButton=new JButton();
        okButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dealOkButton();
                    ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
                }
            });

        JButton backButton=new JButton();
        backButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dealBackButton();
                    ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
                }
            });

        JButton homeButton=runInfo.createHomeButton();
        JPanel topPanel=ElfiiCommonUtil.createTopPanel("系统设置",backButton,homeButton);
        JPanel bottomPanel=ElfiiCommonUtil.createBottomPanelLevelSelect(okButton);
        okButton.setVisible(false);
        createConfigPanel();
        mainPanel.setLayout(null);
        mainPanel.setBackground(Color.black);
        mainPanel.add(topPanel);
        topPanel.setBounds(0,0,1024,100);
        mainPanel.add(bottomPanel);
        bottomPanel.setBounds(0,668,1024,100);
        mainPanel.add(configPanel);
        configPanel.setBounds(0,101,1024,560);
		Timer timer = new Timer();
		
		timer.schedule(new TimerTask(){
			public void run() {
				// TODO Auto-generated method stub
				initNextPanel();
			}
		}, 100);
		

		return mainPanel;
	}
	
	public void initNextPanel(){
		initLawWindowPanel();
		initLicenseWindowPanel();
	}
	
	public void initLawWindowPanel(){

        JButton lawYesButton=new JButton();
        ImageIcon backIcon = new ImageIcon("resource-common/images/login/sssss06.png","");
        lawYesButton.setBackground(Color.white);
        lawYesButton.setBorderPainted(false);
        lawYesButton.setFocusPainted(false);
        lawYesButton.setIcon(backIcon);
        lawYesButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    mainFrame.setContentPane(mainPanel);
                    mainFrame.validate();
                    ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
                }
            });

        lawYesButton.setBackground(Color.darkGray);
        lawYesButton.setContentAreaFilled(false);

        lawWindowPanel= new LawWindowPanel();
        lawWindowPanel.setLayout(null);
        lawWindowPanel.add(lawYesButton);
        lawYesButton.setBounds(450,450,121,43);
	}
	
	public void initLicenseWindowPanel(){

		  JButton licenseYesButton=new JButton();
          ImageIcon backIcon = new ImageIcon("resource-common/images/login/sssss06.png","");
          licenseYesButton.setBackground(Color.white);
          licenseYesButton.setBorderPainted(false);
          licenseYesButton.setFocusPainted(false);
          licenseYesButton.setIcon(backIcon);
          licenseYesButton.addActionListener( new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      mainFrame.setContentPane(mainPanel);
                      mainFrame.validate();
                      ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
                  }
              });

          licenseYesButton.setBackground(Color.darkGray);
          licenseYesButton.setContentAreaFilled(false);

          licenseWindowPanel= new LicenseWindowPanel();
          licenseWindowPanel.setLayout(null);
          licenseWindowPanel.add(licenseYesButton);
          licenseYesButton.setBounds(450,450,121,43);
	}
}
